package com.kk.blog.controller.admin;

import com.kk.blog.pojo.Blog;
import com.kk.blog.pojo.User;
import com.kk.blog.service.BlogSrevice;
import com.kk.blog.service.TagService;
import com.kk.blog.service.TypeService;
import com.kk.blog.vo.BlogQuery;
import org.apache.ibatis.javassist.NotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class BlogController {

    @Autowired
    private BlogSrevice blogSrevice;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;


    //blog列表
    @GetMapping("/blogs")
    public String blogs(@PageableDefault(size = 3, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                        BlogQuery blog, Model model) {

        model.addAttribute("types", typeService.listType());
        Page<Blog> blogs = blogSrevice.listBlog(pageable, blog);
        model.addAttribute("page", blogs);

        return "admin/blogs";
    }


    //分页blog
    @PostMapping("/blogs/search")
    public String search(@PageableDefault(size = 3, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                         BlogQuery blog, Model model) {

        Page<Blog> blogs = blogSrevice.listBlog(pageable, blog);
        model.addAttribute("page", blogs);

        return "admin/blogs :: blogList ";
    }

//    @GetMapping( "/blogs/input")
//    public String input(Model model){
//        model.addAttribute(  "types", typeService.listType());
//        model.addAttribute( "tags", tagService.listTag());
//        model.addAttribute( "blog" , new Blog());
//
//        return "admin/blogs-input";
//    }


    //进入blog-input页面提交的地址方法
    @GetMapping("/blogs/input")
    public String input(Model model) {
        setTypeAndTag(model);
        model.addAttribute("blog", new Blog());
        return "admin/blogs-input";
    }

    private void setTypeAndTag(Model model) {
        model.addAttribute("types", typeService.listType());
        model.addAttribute("tags", tagService.listTag());
    }


    //点击编辑按钮的逻辑方法，并回显编辑信息
    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Long id, Model model) {
        setTypeAndTag(model);
        Blog blog = blogSrevice.getBlog(id);
        blog.init();
        model.addAttribute("blog", blog);

        return "admin/blogs-input";
    }


    //blog-input页面提交的的逻辑方法
    @PostMapping("/blogs")
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session) throws NotFoundException {
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagIds()));
        Blog b;
        if (blog.getId() == null) {
            b =  blogSrevice.saveBlog(blog);
        } else {
            b = blogSrevice.updateBlog(blog.getId(), blog);
        }

        if (b == null) {
            attributes.addFlashAttribute("message", "操作失败");
        } else {
            attributes.addFlashAttribute("message", "操作成功");
        }
        return "redirect:/admin/blogs";
    }


    //删除blog
    @GetMapping("/blogs/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes) {
        blogSrevice.deleteBlog(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/blogs";
    }


}
